# Contributor: Antoine Martin (ayakael) <dev@ayakael.net>
# Maintainer: Antoine Martin (ayakael) <dev@ayakael.net>
pkgname=zotero
pkgver=7.0.9
pkgrel=0
_fxver=115.16.0
_gittag=$pkgver
pkgdesc="A free, easy-to-use tool to help you collect, organize, cite, and share your research sources."
url="https://www.zotero.org/"
# s390x and riscv64: blocked by rust and cargo
# armhf: build failure on armhf due to wasm
# ppc64le: test suite stalls
arch="x86_64 armv7 aarch64 x86"
license="GPL-3.0-only AND LGPL-2.1-only AND LGPL-3.0-only AND MPL-2.0"
depends="
	ffmpeg-libavcodec
	"
_llvmver=18
makedepends="
	alsa-lib-dev
	automake
	bash
	bsd-compat-headers
	cargo
	cbindgen
	clang$_llvmver
	clang$_llvmver-libclang
	curl
	dbus-glib-dev
	gettext
	grep
	gtk+3.0-dev
	hunspell-dev
	icu-dev>=69.1
	libevent-dev
	libffi-dev
	libjpeg-turbo-dev
	libnotify-dev
	libogg-dev
	libtheora-dev
	libtool
	libvorbis-dev
	libvpx-dev
	libwebp-dev
	libxcomposite-dev
	libxt-dev
	lld$_llvmver
	llvm$_llvmver-dev
	m4
	mesa-dev
	nasm
	nodejs
	npm
	nspr-dev
	nss-dev
	perl
	pipewire-dev
	pulseaudio-dev
	python3
	rsync
	sed
	unzip
	wireless-tools-dev
	xvfb-run
	zip
	"
source="https://ftp.mozilla.org/pub/firefox/releases/${_fxver}esr/source/firefox-${_fxver}esr.source.tar.xz
	audio-lfs64.patch
	disable-moz-stackwalk.patch
	fix-fortify-system-wrappers.patch
	fix-rust-target.patch
	fix-webrtc-glibcisms.patch
	icu74.patch
	lfs64.patch
	llvm18-bindgen.patch
	llvm18.patch
	moz-system-jpeg.patch
	no-ccache-stats.patch
	nrappkit-qsort.patch
	ppc-musttail.patch
	ppc-webrtc.patch
	python-deps.patch
	rust-lto-thin.patch
	rust1.78-packed-to-portable_simd.patch
	sandbox-fork.patch
	sandbox-largefile.patch
	sandbox-sched_setscheduler.patch

	stab.h

	mozilla-location.keys
	vendor-prefs.js

	zotero.desktop
	https://ayakael.net/api/packages/mirrors/generic/zotero/$pkgver/zotero-$pkgver.tar.gz
	zotero_build-modifications.patch
	zotero_test-drop-build.patch
	zotero_test-fix-chars.patch
	zotero_test-push-timeout-to-30sec.patch
	zotero_drop-jazzer.patch
	"

builddir="$srcdir"/firefox-$_fxver
_zoterodir="$srcdir"/zotero-$_gittag
_mozappdir=/usr/lib/zotero

# help our shared-object scanner to find the libs
ldpath="$_mozappdir"

# we need this because cargo verifies checksums of all files in vendor
# crates when it builds and gives us no way to override or update the
# file sanely... so just clear out the file list
_clear_vendor_checksums() {
	sed -i 's/\("files":{\)[^}]*/\1/' third_party/rust/$1/.cargo-checksum.json
}

export SHELL=/bin/sh
export BUILD_OFFICIAL=1
export MOZILLA_OFFICIAL=1
export USE_SHORT_LIBNAME=1
export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=system
export MOZ_APP_PROFILE="mozilla/firefox"
export MOZ_APP_REMOTINGNAME=firefox-esr
export MOZBUILD_STATE_PATH="$srcdir"/mozbuild
# disable desktop notifications
export MOZ_NOSPAM=1
# Find our triplet JSON
export RUST_TARGET="$CTARGET"

# Build with Clang, takes less RAM
export CC="clang-$_llvmver"
export CXX="clang++-$_llvmver"

# set rpath so linker finds the libs
export LDFLAGS="$LDFLAGS -Wl,-rpath,$_mozappdir"

# let firefox do this itself.
unset CARGO_PROFILE_RELEASE_OPT_LEVEL
unset CARGO_PROFILE_RELEASE_LTO

export CFLAGS="${CFLAGS/-fstack-clash-protection/} -g0 -O2"
export CXXFLAGS="${CXXFLAGS/-fstack-clash-protection/} -g0 -O2 -Wno-deprecated-builtins -Wno-deprecated-declarations"

prepare() {
	# zotero prepare
	mv "$_zoterodir" "$builddir"/zotero
	(
		cd "$builddir"/zotero

		# checks that we're using expected fx version
		local _exp_fxver=$(grep GECKO_VERSION_LINUX "$builddir"/zotero/app/config.sh | sed 's|.*=||' | tr -d '"' | sed 's|esr||')
		if [ "$_fxver" != "$_exp_fxver" ]; then
			msg "Expected firefox version $_exp_fxver, got $_fxver"
		fi

		# zotero build expects to be in a git repo
		git init
		git config user.name info
		git config user.email info@example.org
		git commit --allow-empty -m 'Initial'

		npm i --legacy-peer-deps
	)
	default_prepare

	rm -rf third_party/rust/packed_simd/
	cp "$srcdir"/stab.h toolkit/crashreporter/google-breakpad/src/

	_clear_vendor_checksums audio_thread_priority
	_clear_vendor_checksums bindgen

	base64 -d "$srcdir"/mozilla-location.keys > "$builddir"/mozilla-api-key

	# webrtc does not build on these
	case "$CARCH" in
	ppc64le)
		local webrtc_config="ac_add_options --disable-webrtc"
		;;
	esac
	case "$CARCH" in
	armv7)
		# broken here
		local rust_simd="ac_add_options --disable-rust-simd"
		;;
	*)
		local rust_simd="ac_add_options --enable-rust-simd"
		;;
	esac

	case "$CARCH" in
	aarch64|arm*|x86*)
		# disable-elf-hack: exists only on aarch64, arm*, x86, x86_64
		local arch_config="ac_add_options --disable-elf-hack"
		;;
	esac

	# sandbox only supported here
	case "$CARCH" in
	x86*|armv7|aarch64)
		local sandbox="ac_add_options --enable-sandbox"
		;;
	*)
		local sandbox="ac_add_options --disable-sandbox"
		;;
	esac

	cat > base-mozconfig <<-EOF
	# disable unwanted things
	ac_add_options --disable-bootstrap
	ac_add_options --disable-cargo-incremental
	ac_add_options --disable-crashreporter
	ac_add_options --disable-debug
	ac_add_options --disable-debug-symbols
	ac_add_options --disable-install-strip
	ac_add_options --disable-jemalloc
	ac_add_options --disable-strip
	ac_add_options --disable-tests
	ac_add_options --disable-updater

	# features
	ac_add_options --enable-alsa
	ac_add_options --enable-dbus
	ac_add_options --enable-default-toolkit=cairo-gtk3-wayland
	ac_add_options --enable-ffmpeg
	ac_add_options --enable-hardening
	ac_add_options --enable-linker=lld
	ac_add_options --enable-necko-wifi
	ac_add_options --enable-official-branding
	ac_add_options --enable-optimize="$CFLAGS"
	ac_add_options --enable-pulseaudio
	ac_add_options --enable-release
	ac_add_options --enable-update-channel=release

	# system libs
	ac_add_options --enable-system-pixman
	ac_add_options --with-system-ffi
	ac_add_options --with-system-icu
	ac_add_options --with-system-jpeg
	ac_add_options --with-system-libevent
	ac_add_options --with-system-libvpx
	ac_add_options --with-system-nspr
	ac_add_options --with-system-nss
	ac_add_options --with-system-png
	ac_add_options --with-system-webp
	ac_add_options --with-system-zlib

	# disable wasm due to wasi-sdk and wasi-compiler-rt built against clang19
	# ac_add_options --with-wasi-sysroot=/usr/share/wasi-sysroot
	ac_add_options --without-wasm-sandboxed-libraries

	# misc
	ac_add_options --allow-addon-sideload
	ac_add_options --prefix=/usr
	ac_add_options --with-app-name=firefox-esr
	ac_add_options --with-distribution-id=org.alpinelinux
	ac_add_options --with-libclang-path=/usr/lib
	ac_add_options --with-unsigned-addon-scopes=app,system
	ac_add_options --host=$CHOST
	ac_add_options --target=$CTARGET

	# objdir
	mk_add_options MOZ_OBJDIR="$builddir/obj"

	mk_add_options RUSTFLAGS="$RUSTFLAGS"

	# keys
	# these are for alpine linux use only
	ac_add_options --with-mozilla-api-keyfile="$builddir/mozilla-api-key"

	$arch_config
	$rust_simd
	$sandbox
	$webrtc_config
	EOF
}

build() {
	cat > .mozconfig base-mozconfig

	export MOZ_BUILD_DATE=$(date ${SOURCE_DATE_EPOCH:+ -d@${SOURCE_DATE_EPOCH}} "+%Y%m%d%H%M%S")

	# for lto
	ulimit -n 4096

	# can't be set here and fail
	unset RUSTFLAGS

	local thinlto_jobs=${JOBS:-1}

	case "$CARCH" in
	# on this platforms, lld seems to not utilise >1 threads for thinlto for some reason.
	# at the same time, having more than 8 also crashes lld for firefox buildsystems (why?).
	aarch64)
		if [ $thinlto_jobs -gt 8 ]; then
			thinlto_jobs=8
		fi
		;;
	esac

	export LDFLAGS="$LDFLAGS -Wl,--thinlto-jobs=$thinlto_jobs"

	case "$CARCH" in
	# lto for 64-bit systems only
	aarch64|x86_64|ppc64le)
		cat > .mozconfig base-mozconfig <<-EOF
		ac_add_options --enable-lto=cross
		EOF
	esac

 	./mach build

	# install to where zotero expects it
	DESTDIR="$builddir"/zotero/app/xulrunner/firefox ./mach install
	mv "$builddir"/zotero/app/xulrunner/firefox/usr/lib/firefox-esr/* "$builddir"/zotero/app/xulrunner/firefox/.
	rm -R "$builddir"/zotero/app/xulrunner/firefox/usr

	# zotero build
	(
		cd "$builddir"/zotero
		NODE_OPTIONS=--openssl-legacy-provider npm run build

		SKIP_32=1 app/scripts/dir_build -p l
		# move to seperate folder as check will rebuild with test files added
		mv "$builddir"/zotero/app/staging  "$builddir"/zotero/app/package
	)

	rm -R "$builddir"/zotero/app/xulrunner/firefox # clean-up unused firefox install
}

check() {
	# install to where zotero expects it
	DESTDIR="$builddir"/zotero/app/xulrunner/firefox ./mach install
	mv "$builddir"/zotero/app/xulrunner/firefox/usr/lib/firefox-esr/* "$builddir"/zotero/app/xulrunner/firefox/.
	rm -R "$builddir"/zotero/app/xulrunner/firefox/usr

	# zotero test build
	(
		cd "$builddir"/zotero
		ZOTERO_TEST=1 SKIP_32=1 app/scripts/dir_build -p l

	)

	# test-suite is not very stable, false failure occur from time to time, thus never fails
	CI=true LD_LIBRARY_PATH="$builddir"/zotero/app/staging/Zotero_linux xvfb-run "$builddir"/zotero/test/runtests.sh -x "$builddir"/zotero/app/staging/Zotero_linux/zotero || true

	rm -R "$builddir"/zotero/app/xulrunner/firefox # clean-up unused firefox install
}

package() {
	install -dDm755 "$pkgdir"/usr/bin
	install -dDm755 "$pkgdir"/usr/lib/zotero
	cp -r "$builddir"/zotero/app/package/Zotero_linux/* "$pkgdir/usr/lib/zotero"
	ln -s /usr/lib/zotero/zotero "$pkgdir/usr/bin/zotero"
	install -Dm644 "$srcdir/zotero.desktop" "$pkgdir/usr/share/applications/zotero.desktop"

	# Copy zotero icons to a standard location
	install -Dm644 "$pkgdir/usr/lib/zotero/icons/icon32.png" "$pkgdir/usr/share/icons/hicolor/32x32/apps/zotero.png"
	install -Dm644 "$pkgdir/usr/lib/zotero/icons/icon64.png" "$pkgdir/usr/share/icons/hicolor/64x64/apps/zotero.png"
	install -Dm644 "$pkgdir/usr/lib/zotero/icons/icon128.png" "$pkgdir/usr/share/icons/hicolor/128x128/apps/zotero.png"

	# Close shell when launching
	sed -i -r 's:^("\$CALLDIR/zotero-bin" -app "\$CALLDIR/application.ini" "\$@"):exec \1:' "$pkgdir/usr/lib/zotero/zotero"
}

sha512sums="
b6f93ec3d6acac5df177253c65b833c017e65ed6e78e96ff029098443928d291f6f67164aedb83d80aa28ee9dee305086597798238d3330e35183030e53e3550  firefox-115.16.0esr.source.tar.xz
3e0501ae7a650346c667dfdc0ae0ca286084f22e89ab2ac671cc0d7315673dc5b6dcb9f9882f6f39d26e9a31e57f7a0fd53d6b805e520224e22b8976850e2eb8  audio-lfs64.patch
454ea3263cabce099accbdc47aaf83be26a19f8b5a4568c01a7ef0384601cf8315efd86cd917f9c8bf419c2c845db89a905f3ff9a8eb0c8e41042e93aa96a85c  disable-moz-stackwalk.patch
2f4f15974d52de4bb273b62a332d13620945d284bbc6fe6bd0a1f58ff7388443bc1d3bf9c82cc31a8527aad92b0cd3a1bc41d0af5e1800e0dcbd7033e58ffd71  fix-fortify-system-wrappers.patch
cd68b89e29e5f6379fbd5679db27b9a5ef70ea65e51c0d0a8137e1f1fd210e35a8cfb047798e9549bc7275606d7ec5c8d8af1335d29da4699db7acd8bc7ff556  fix-rust-target.patch
305c874fdea3096e9c4c6aa6520ac64bb1c347c4b59db8360096646593fe684c3b5377874d91cecd33d56d1410b4714fbdea2b514923723ecbeff79d51265d9b  fix-webrtc-glibcisms.patch
afabea91b328c5a68eaa20f9099ac7b2d0e7f2423e816b05ed168bdd326a5684fa02de08bf05c6033e9b888f02775d1b0443a00329b7a632ee399122a391c13a  icu74.patch
5fa9382c692e4bd6a2634308f24a6526fd12a60a2563d2090056d43a60505df3ec9881bbf54562e69394467529b3b0dc45955afca46ed329af03cea074fff070  lfs64.patch
d7a9443cd81d3ef4a6baa1b5d05f2497bc07c1de815a4d85b613b734989e7c589019dd860ea2eb9e32067973efbfd1c913790c6c238778da23d76fbd904128d1  llvm18-bindgen.patch
8c4de34188f634833d99d17bc389c43ab9576b3f0581387407ae77f636a890779cd02786b632fbdfd8b0305575517d51b04f76b9aa3f1a94c3d822578286fc6f  llvm18.patch
e28b1fe2967d46c888b41a0a03ee1b6de763124a0281b68aebac04ec3b76cb6fdb56a376d419a003fd87f79e9d086aafd4aaa24efa3345f9857352d3d489356f  moz-system-jpeg.patch
c0437a6753f3f350968fa12d250efdfe1bea77baf0e4c06b072b5cc9e78c774dbf4506bc536337030d349fb3ba4460097b75b0c7c5b8fb2d39d8b0a392948936  no-ccache-stats.patch
b586ddb21b50fa8fba798c3a269518049c38cc52fd91ad00e96a3f1144e6b2879d9df1a182a2fb181898042043ae98381a70d0e4acbf8aa705d2b4f62c17a5a1  nrappkit-qsort.patch
2d8dff86212d6d2a904cbb5a5a1d6c17b89adc929fc6a3f4c6cb669f5e83ecddff5a799225319ba445a187b04d111251af75dd3ce8a039164bc14d2a432a2a04  ppc-musttail.patch
6f60e83599041db1b707c21784197ea9816b2c936b89a274bfc24554a600981e6f28448fe41fab0942bd31acd49b1c00beb2eb0961149f2ffa6a4154be123ea7  ppc-webrtc.patch
4e40b34c5f77a1a21fe971a6fcd8a21b1a63423a3a7932a5a6e1c7a2779f9f06a561c806614a01931679a4b1c6afdfd8ae1f3cc6b673f259ccd368e8e54f6e90  python-deps.patch
1c6918dd6655d3a1251bfd4af2e1c561cbb00d540a883b4c1ebf7f5de530d754d9ac07b4b5f56cdab6c511d25c8910ec94043f5733e97501a67abffe1bafaeb1  rust-lto-thin.patch
e4795b88e572bb3b6f009dbacc7872b59db2f8facbe2b07f14dd4f45a77c22049783581869f18c6bb3f04d6edb3da59347278321cce2bfa629a4d35e136cda5c  rust1.78-packed-to-portable_simd.patch
2518f2fc75b5db30058e0735f47d60fdf1e7adfaeee4b33fb2afb1bd9a616ce943fd88f4404d0802d4083703f4acf1d5ad42377218d025bc768807fbaf7e1609  sandbox-fork.patch
b7d0a6126bdf6c0569f80aabf5b37ed2c7a35712eb8a0404a2d85381552f5555d4f97d213ea26cec6a45dc2785f22439376ed5f8e78b4fd664ef0223307b333e  sandbox-largefile.patch
f8c3555ef6207933cbffbf4fc101a9b4c0d2990c0063162f0f0bde70ef0b46f86bfac42e7110695183424a87948de593f3927b2d8509ede3e4fc7bd8a1fad1ce  sandbox-sched_setscheduler.patch
0b3f1e4b9fdc868e4738b5c81fd6c6128ce8885b260affcb9a65ff9d164d7232626ce1291aaea70132b3e3124f5e13fef4d39326b8e7173e362a823722a85127  stab.h
382510375b1a2fa79be0ab79e3391a021ae2c022429ffbaa7e7a69166f99bb56d01e59a1b10688592a29238f21c9d6977672bd77f9fae439b66bdfe0c55ddb15  mozilla-location.keys
fc45bc3ffb9404e5338ea26a9f04807b40f6f516324972cddd48bedb91b8bd7c6b8d4e03a0209020f5e67b703bc4ff89389985791b9bd544a0fc3951e2dc338e  vendor-prefs.js
e1a0a4ff5cc1b53f13776ca11927d671426b0691e78e74a4adf2166d57bb2ae8ac409cc11a37ce5e2f680fdf05d5bc3849c33a9717aca1bb62d03ae5231a67fb  zotero.desktop
97955d89ab108a61d0db7d04d4a9d11cc5a97907e8b82232a836613b20e18f89eff2c2c69f4492cb49c014d129767e4d5fd5b46b68b2b05d38d454bc04eea337  zotero-7.0.9.tar.gz
746dbabbb3ea9199d17891e2079b9256d04843f548132178862117d2334694d98e2cc981945d72f31d0e5b2c42904d371633f6905996bb580aa0b5ae95c64ddc  zotero_build-modifications.patch
337070ee4c44ccb35c6b6290c18327740bb9fccfd1a6ad1045782e83daa290b6ced7d53955d3a889f661d588738a64f2e7f383639f4c46be9fdf891168abc9ff  zotero_test-drop-build.patch
f0e4f09496531222e8400959f9ef12852bca269eb3bf4c3b87ccaf92f28a12b9374461a1c79ad294a4393dbe30800aa1c85497033d0bc304fa8c198dfab3efd2  zotero_test-fix-chars.patch
9dc390d8bae42e645cae45fe5551751d8f38d5c1b8b2cc0eec1c2191f4bde293ffb2c67cfc4de765f2d48b1da4d5fcd4f1c03711e5da3180cd1b63710ccf5599  zotero_test-push-timeout-to-30sec.patch
3730281d9dc7f6b08c8ac31f057fd2baf6e1263835012959bd3c7118c3cd8b4e672513eb9cb7df0d97904207505a98c57465e307649d2ee6ff900b45401bce31  zotero_drop-jazzer.patch
"
